package com.sy.server.util;

public class DateUtil {
    private int year;
    private int month;
    private static int[] days=new int[]{31,28,31,30,31,30,31,31,30,31,30,31};
    public static int getDays(int year,int month){
        boolean flag=judgeYear(year);
        return month==2 && flag ? 29:days[month-1];
    }

    /**
     * 得到某年某月的起止时间
     * @param year
     * @param month
     * @return
     */
    public static String[] getBeginEndDay(Integer year, Integer month){//2020 5
        int days=DateUtil.getDays(year,month);//31
        String beginDate=daysToString(year,month,1);//2020-05-01
        String endDate=daysToString(year,month,days);//2020-05-31
        return new String[]{beginDate,endDate};
    }

    public static String daysToString(Integer year, Integer month, Integer day){
        String monthToString=month.toString();
        String dayToString=day.toString();
        if(month<10){
            monthToString="0"+monthToString;
        }
        if(day<10){
            dayToString="0"+dayToString;
        }
        return year+"-"+monthToString+"-"+dayToString;
    }

    /**
     * 根据传入参数确定不同的起止时间，
     * 如传入year=0,month=0   则起止时间是2000-01-01 -->2099-12-31
     *  传入year!=0,month=0   则起止时间是year-01-01 -->year-12-31
     *  传入year!=0,month!=0  则起止时间是year-month-01 -->year-month-31/30/28/29
     * @param year
     * @param month
     * @return
     */
    public static String[] processingParams(Integer year,Integer month){
        String beginDate,endDate;
        if(year==0 && month==0){
            beginDate="2000-01-01";
            endDate="2099-12-31";
        }else if(year!=0 && month==0){
            beginDate=getBeginEndDay(year,1)[0];
            endDate=getBeginEndDay(year,12)[1];
        }else{
            beginDate=getBeginEndDay(year,month)[0];
            endDate=getBeginEndDay(year,month)[1];
        }
        return new String[]{beginDate,endDate};
    }

    private static boolean judgeYear(int year){
        return year%4==0 && year%100!=0 || year%400==0;
    }
}
